﻿using EFCore.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SIService;
using SService;

namespace WMSWeb.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ProductTypeController : ControllerBase
    {
        private readonly WmssystemContext myDbContext;
        private readonly ProductTypeS ProductTypeS;

        public ProductTypeController(WmssystemContext myDbContext, ProductTypeS ProductTypeS)
        {
            this.myDbContext = myDbContext;
            this.ProductTypeS = ProductTypeS;
        }
        /// <summary>
        /// 货品类型分页模糊查询
        /// </summary>
        /// <returns></returns>
        [HttpGet("FenYe")]
        public async Task<List<PO_ProductType>> FenYe(int pageSize, int currentPage, string Key = null, bool? Status = null, string Operator = null, DateTime? OperationTime = null)
        {
            return await ProductTypeS.FenYe(pageSize, currentPage, Key, Status, Operator, OperationTime);
        }
        /// <summary>
        /// 货品类型添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult<int>> Add(PO_ProductType proAdd)
        {
            // 为实体的属性赋值
            proAdd.P_PIsDelete = true;
            proAdd.P_ProductTypeOperationTime = DateTime.Now;
            proAdd.P_ProductTypeOperator = "张三";
            // 调用服务层的方法执行添加操作
            return await ProductTypeS.Add(proAdd);
        }
        /// <summary>
        /// 货品类型修改
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        public async Task<ActionResult<int>> Update(PO_ProductType proUpd)
        {
            return await ProductTypeS.Upd(proUpd);
        }
        /// <summary>
        /// 货品类型逻辑删除
        /// </summary>
        /// <returns></returns>
        [HttpPut("UpdIsDelete")]
        public async Task<ActionResult<int>> UpdIsDelete(int id, bool isDeleted)
        {
            return await ProductTypeS.UpdIsDelete(id, isDeleted);
        }
        /// <summary>
        /// 货品类型级联
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<Sys_jilian>> GetYI()
        {
            List<PO_ProductType> topLevelItems = await ProductTypeS.Yiji();
            List<Sys_jilian> jilians = new List<Sys_jilian>();
            foreach (PO_ProductType item in topLevelItems)
            {
                Sys_jilian jilian = new Sys_jilian()
                {
                    value = (int)item.P_ProductTypeId,
                    label = item.P_ProductTypeName,
                    children = await GetChildren(item.P_ProductTypeId)
                };
                jilians.Add(jilian);
            }
            return jilians;
        }

        private async Task<List<Sys_jilian>> GetChildren(int? parentId)
        {
            List<PO_ProductType> childItems = await ProductTypeS.Erji((int)parentId);
            List<Sys_jilian> jilians = new List<Sys_jilian>();
            foreach (PO_ProductType item in childItems)
            {
                // 检查是否已经存在相同的节点
                if (!jilians.Any(x => x.value == item.P_ProductTypeId))
                {
                    Sys_jilian jilian = new Sys_jilian()
                    {
                        value = (int)item.P_ProductTypeId,
                        label = item.P_ProductTypeName,
                        children = await GetChildren(item.P_ProductTypeId)
                    };
                    jilians.Add(jilian);
                }
            }
            return jilians;
        }

    }
}
